﻿package net.wangit.framework.interceptor;

import net.wangit.Invocation;
import net.wangit.Page;
import net.wangit.context.ActionContext;
import net.wangit.framework.action.ActionMapping;
import net.wangit.framework.dispatcher.DefaultInvocation;
import net.wangit.framework.log.UserLogger;
import net.wangit.support.ActionInterceptor;
import net.wangit.support.PageInterceptor;
import net.wangit.view.page.PageImpl;
/**
 * 记录用户日志，包括Action执行和页面访问
 * @author watson
 *
 */
public class UserLogInterceptor implements ActionInterceptor, PageInterceptor {

	public UserLogInterceptor() {
	}

	public void intercept(ActionContext ac, Invocation invocation)
			throws Exception {
		ActionMapping mapping = ((DefaultInvocation) invocation)
				.getActionMapping();
		long start = System.currentTimeMillis();
		try {
			invocation.invoke();
			if (mapping.isSupportLog())
				UserLogger.log(ac, mapping, System.currentTimeMillis() - start,
						true);
		} catch (Exception e) {
			if (mapping.isSupportLog())
				UserLogger.log(ac, mapping, System.currentTimeMillis() - start,
						false);
			throw e;
		}
	}

	public void intercept(ActionContext ac, Page page) throws Exception {
		long start = System.currentTimeMillis();
		try {
			page.write(ac);
			if (((PageImpl) page).isSupportLog())
				UserLogger.log(ac, (PageImpl) page, System.currentTimeMillis()
						- start, true);
		} catch (Exception e) {
			UserLogger.log(ac, (PageImpl) page, System.currentTimeMillis()
					- start, false);
			throw e;
		}
	}
}
